import socketserver

class TcpServer(socketserver.BaseRequestHandler):
    """
    必须继承socketserver.BaseRequestHandler类
    """
    def handle(self):
        """
        必须实现这个方法！
        :return:
        """
        self.conn = self  # 将handler实例赋值给self.conn
        print('=================', self.conn)
        conn = self.request         # request里封装了所有请求的数据
        conn.sendall('欢迎访问socketserver服务器！'.encode())
        while True:
            data = conn.recv(1024).decode()
            if data == "exit":
                print("断开与%s的连接！" % (self.client_address,))
                break
            print("来自%s的客户端向你发来信息：%s" % (self.client_address, data))
            conn.sendall(('已收到你的消息<%s>' % data).encode())


class MyServer:
    def __init__(self):
        self.conn = None
        self.setup()
    def setup(self):
        # 创建一个多线程TCP服务器
        server = socketserver.ThreadingTCPServer(('127.0.0.1', 9999), TcpServer)
        print("启动socketserver服务器！")
        # 启动服务器，服务器将一直保持运行状态
        server.serve_forever()
